package com.fpf.controller;

import com.fpf.controller.advice.NotControllerResponseAdvice;
import com.fpf.controller.bo.ProductInfoBO;
import com.fpf.controller.vo.request.ProductInfoVO;
import com.fpf.controller.vo.response.APIException;
import com.fpf.controller.vo.response.ResultVO;
import com.fpf.service.ProductInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ApiController {

    @Autowired
    private ProductInfoService productInfoService;

    @PostMapping("/findByVo1")
    public String findByVoString(@Validated @RequestBody ProductInfoVO vo) {
        ProductInfoBO productInfo = new ProductInfoBO();
        BeanUtils.copyProperties(vo, productInfo);
        return productInfoService.getOne(vo).getProductName();
    }
    @PostMapping("/findByVo2")
    public ProductInfoVO findByVo(@Validated @RequestBody ProductInfoVO vo) {
        ProductInfoBO productInfo = new ProductInfoBO();
        BeanUtils.copyProperties(vo, productInfo);
        return productInfoService.getOne(vo);
    }

    @PostMapping("/findByVo3")
    public ProductInfoVO businessException(@Validated @RequestBody ProductInfoVO vo) {
        ProductInfoBO productInfo = new ProductInfoBO();
        if(vo!=null)throw new APIException("故意触发APIException");
        return productInfoService.getOne(vo);
    }

    @NotControllerResponseAdvice
    @GetMapping("health")
    public String health(){
        return "success";
    }

}
